/*
 * game.cpp
 *
 *  Created on: 08.11.2014
 *      Author: Siegenni
 */

#include "game.hpp"
#include <math.h>
#include <iostream>


Game::Game()
: _window(sf::VideoMode(1280, 720), "dodger", sf::Style::Close | sf::Style::Titlebar),
  _myPlayer("mein Spieler", "res\\player.png", 500, 500),
  _enemies(),
  _bullets()
{
	tick();
}

void Game::tick()
{
	sf::Clock loopTimer, enemyTimer, timeSinceStarted;
	sf::Time elapsed;
	const int loopsPerSec = 30;
	float enemiesPerSecond = 0.5;

	while (_window.isOpen())
	{
		sf::Event event;
	    while (_window.pollEvent(event))
	    {
	        if (event.type == sf::Event::Closed)
	        _window.close();
	    }

	    elapsed = loopTimer.getElapsedTime();
	    if (elapsed.asMilliseconds() > (1000 / loopsPerSec))
	    {
	        getKeys();

	        for (int i = 0; i < (int) _enemies.size(); i++)
	        {
	        	if (_enemies.at(i).getPosition().y > (int) _window.getSize().y) _enemies.erase(_enemies.begin() + i);
	        	_enemies.at(i).setPositionRel(0, _enemies.at(i).getMovementSpeed());
	        }

	        for (int i = 0; i < (int) _bullets.size(); i++)
	        {
	        	if (_bullets.at(i).getPosition().y > (int) _window.getSize().y) _bullets.erase(_bullets.begin() + i);
	        	_bullets.at(i).setPositionRel(0, -5);

	        	for (int e = 0; e < (int) _enemies.size(); e++)
	        	{
	        		if (_enemies.at(e).getPosition().x <= 								_bullets.at(i).getPosition().x &&
	        			_enemies.at(e).getPosition().x + _enemies.at(e).getSize().x >= 	_bullets.at(i).getPosition().x &&
	        			_enemies.at(e).getPosition().y <= 								_bullets.at(i).getPosition().y &&
	        			_enemies.at(e).getPosition().y + _enemies.at(e).getSize().y >= 	_bullets.at(i).getPosition().y)
	        		{
	        			_enemies.erase(_enemies.begin() + e);
	        			_bullets.erase(_bullets.begin() + i);
	        		}

	        		std::cout << "EnemyPosX <= BulletPosX " << (_enemies.at(e).getPosition().x <= _bullets.at(i).getPosition().x) << std::endl;
	        		std::cout << "EnemyPosX+EnemySizeX >= BulletPosX " << (_enemies.at(e).getPosition().x + _enemies.at(e).getSize().x >= _bullets.at(i).getPosition().x) << std::endl;
	        		std::cout << "EnemyPosY <= BulletPosY " << (_enemies.at(e).getPosition().y <= _bullets.at(i).getPosition().y) << std::endl;
	        		std::cout << "EnemyPosY+EnemySizeY >= BulletPosY " << (_enemies.at(e).getPosition().y + _enemies.at(e).getSize().y >= _bullets.at(i).getPosition().y) << std::endl << std::endl;
	        	}
	        }

	        elapsed = enemyTimer.getElapsedTime();
	        if (enemiesPerSecond < 4.5) enemiesPerSecond = 0.1 * timeSinceStarted.getElapsedTime().asSeconds() + 0.5;
	        if (elapsed.asMilliseconds() > (1000 / enemiesPerSecond))
	        {
	        	_enemies.push_back(Entity("mein Spieler", "res\\enemy.png", rand() % _window.getSize().x, -48));
	        	_enemies.at(_enemies.size()-1).setMovementSpeed(rand() % 5);

	        	enemyTimer.restart();
	        }

	        loopTimer.restart();
	    }

	    _window.clear();
	    _window.draw(_myPlayer.getSprite());

	    for (int i = 0; i < (int) _enemies.size(); i++)
	    {
	    	sf::Sprite tmpSprite(_enemies.at(i).getTexture());
	    	tmpSprite.setPosition(_enemies.at(i).getPosition().x, _enemies.at(i).getPosition().y);
	    	tmpSprite.setScale(_enemies.at(i).getScale(), _enemies.at(i).getScale());
	    	_window.draw(tmpSprite);
	    }

	    for (int i = 0; i < (int) _bullets.size(); i++)
	    {
	    	sf::Sprite tmpSprite(_bullets.at(i).getTexture());
	    	tmpSprite.setPosition(_bullets.at(i).getPosition().x, _bullets.at(i).getPosition().y);
	    	tmpSprite.setScale(_bullets.at(i).getScale(), _bullets.at(i).getScale());
	    	_window.draw(tmpSprite);
	    }

	    _window.display();
	}
}

void Game::getKeys()
{
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && _myPlayer.getPosition().x > -1) _myPlayer.setPositionRel(-7, 0);
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && _myPlayer.getPosition().x < _window.getSize().x - _myPlayer.getSprite().getTexture()->getSize().x) _myPlayer.setPositionRel(7, 0);
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) _bullets.push_back(Entity("bullet", "res\\bullet.png", _myPlayer.getPosition().x + _myPlayer.getTexture().getSize().x / 2, _myPlayer.getPosition().y));
}
